package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.DonationProject;
import com.tju.ocean_ecology_website.dao.DonationProjectDao;
import com.tju.ocean_ecology_website.service.DonationProjectService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 捐赠项目表(DonationProject)表服务实现类
 *
 * @author shaolong
 * @since 2025-04-29 19:30:41
 */
@Service("donationProjectService")
public class DonationProjectServiceImpl implements DonationProjectService {
    @Resource
    private DonationProjectDao donationProjectDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public DonationProject queryById(Long id) {
        return this.donationProjectDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param donationProject 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<DonationProject> queryByPage(DonationProject donationProject, PageRequest pageRequest) {
        long total = this.donationProjectDao.count(donationProject);
        return new PageImpl<>(this.donationProjectDao.queryAllByLimit(donationProject, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param donationProject 实例对象
     * @return 实例对象
     */
    @Override
    public DonationProject insert(DonationProject donationProject) {
        this.donationProjectDao.insert(donationProject);
        return donationProject;
    }

    /**
     * 修改数据
     *
     * @param donationProject 实例对象
     * @return 实例对象
     */
    @Override
    public DonationProject update(DonationProject donationProject) {
        this.donationProjectDao.update(donationProject);
        return this.queryById(donationProject.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.donationProjectDao.deleteById(id) > 0;
    }
}
